//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if _MSC_VER > 1000
#pragma once
#endif

#ifndef __SHARED_MEMORY_H__
#define __SHARED_MEMORY_H__

#include <elasys_server.h>
#include <interlocked.h>

class CSharedMemory : public ISharedMemory
{
public:
    CARAPI_(PInterface) Probe(
        /* [in]  */ REIID riid);

    CARAPI_(UInt32)AddRef();

    CARAPI_(UInt32)Release();

    CARAPI Aggregate(
        /* [in]  */ AggregateType type,
        /* [in]  */ PInterface pObject);

    CARAPI GetDomain(
        /* [out] */ PInterface * ppObj);

    CARAPI Attach(
        /* [in]  */ MemoryProtection protect,
        /* [out] */ Address * pAddress);

    CARAPI Detach();

public:
    CSharedMemory() : m_hSharedMemory(NULL),
                  m_address(0),
                  m_attachTimes(0),
                  m_hLock(NULL) {}

    virtual ~CSharedMemory();

    static ECode New(
        /* [in] */ WString name,
        /* [in] */ MemorySize memorySize,
        /* [in] */ Boolean bCommit,
        /* [out] */ ISharedMemory ** ppSharedMemory,
        /* [out] */ Boolean * pbAlreadyExisted);

private:
    ECode Constructor(
        /* [in] */ WString name,
        /* [in] */ MemorySize memorySize,
        /* [in] */ Boolean bCommit,
        /* [out] */ Boolean * pbAlreadyExisted);

private:
    PVoid   m_hSharedMemory;

    Address m_address;
    Int32   m_attachTimes;
    PVoid   m_hLock;

private:
    Interlocked m_cRefs;
};

#endif // __SHARED_MEMORY_H__

